package com.example.openim.service.impl;

import com.example.openim.service.TokenCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Token缓存服务实现类
 * 
 * @author: ZhangHuixin
 * @date: 2025-11-10
 * @description: 实现token的缓存管理，支持用户token和管理员token的缓存
 */
@Slf4j
@Service
public class TokenCacheServiceImpl implements TokenCacheService {
    
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    private static final String USER_TOKEN_KEY_PREFIX = "user_token:";
    private static final String ADMIN_TOKEN_KEY = "admin_token";
    private static final long DEFAULT_TOKEN_EXPIRE = 85L * 24 * 60 * 60; // 85天过期时间
    
    @Override
    public String getUserToken(String userId, Integer platformID) {
        try {
            String key = USER_TOKEN_KEY_PREFIX + userId + ":" + platformID;
            Object token = redisTemplate.opsForValue().get(key);
            if (token != null) {
                log.info("从Redis缓存获取用户token成功，userId: {}, platformID: {}", userId, platformID);
                return token.toString();
            }
            log.info("Redis缓存中未找到用户token，userId: {}, platformID: {}", userId, platformID);
            return null;
        } catch (Exception e) {
            log.error("从Redis获取用户token失败，userId: {}, platformID: {}, error: {}", userId, platformID, e.getMessage());
            return null;
        }
    }
    
    @Override
    public String getAdminToken() {
        try {
            Object token = redisTemplate.opsForValue().get(ADMIN_TOKEN_KEY);
            if (token != null) {
                log.info("从Redis缓存获取管理员token成功");
                return token.toString();
            }
            log.info("Redis缓存中未找到管理员token");
            return null;
        } catch (Exception e) {
            log.error("从Redis获取管理员token失败，error: {}", e.getMessage());
            return null;
        }
    }
    
    @Override
    public void cacheUserToken(String userId, Integer platformID, String token, long expireSeconds) {
        try {
            String key = USER_TOKEN_KEY_PREFIX + userId + ":" + platformID;
            redisTemplate.opsForValue().set(key, token, expireSeconds, TimeUnit.SECONDS);
            log.info("缓存用户token成功，userId: {}, platformID: {}, 过期时间: {}秒", userId, platformID, expireSeconds);
        } catch (Exception e) {
            log.error("缓存用户token失败，userId: {}, platformID: {}, error: {}", userId, platformID, e.getMessage());
        }
    }
    
    @Override
    public void cacheAdminToken(String token, long expireSeconds) {
        try {
            redisTemplate.opsForValue().set(ADMIN_TOKEN_KEY, token, expireSeconds, TimeUnit.SECONDS);
            log.info("缓存管理员token成功，过期时间: {}秒", expireSeconds);
        } catch (Exception e) {
            log.error("缓存管理员token失败，error: {}", e.getMessage());
        }
    }
    
    @Override
    public void clearUserTokenCache(String userId, Integer platformID) {
        try {
            String key = USER_TOKEN_KEY_PREFIX + userId + ":" + platformID;
            redisTemplate.delete(key);
            log.info("清除用户token缓存成功，userId: {}, platformID: {}", userId, platformID);
        } catch (Exception e) {
            log.error("清除用户token缓存失败，userId: {}, platformID: {}, error: {}", userId, platformID, e.getMessage());
        }
    }
    
    @Override
    public void clearAdminTokenCache() {
        try {
            redisTemplate.delete(ADMIN_TOKEN_KEY);
            log.info("清除管理员token缓存成功");
        } catch (Exception e) {
            log.error("清除管理员token缓存失败，error: {}", e.getMessage());
        }
    }
}